package com.ldf.media.api.controller;


import com.ldf.media.api.model.param.StreamProxyParam;
import com.ldf.media.api.model.result.Result;
import com.ldf.media.api.service.IApiService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * NVR摄像头代理接口
 *
 * @author lidaofu
 * @since 2025-07-07
 **/
@Api(tags = "【NVR】摄像头代理接口")
@RestController
@RequestMapping("/nvr/api")
@Validated
@RequiredArgsConstructor
public class NvrCameraController {
    private final IApiService iApiService;

    /**
     * 添加RTSP拉流代理并返回WebRTC推流地址
     */
    @ApiOperation(value = "添加RTSP拉流代理")
    @PostMapping("/startStream")
    public Result<Result> startStream(@Valid @RequestBody StreamProxyParam param) {
        // 调用流媒体服务进行拉流
        Result error = iApiService.addStreamProxy(param);

        if (error != null) {
            return error;
        }
        return new Result<>(null);
        // 构造WebRTC推流地址
//        String webrtcUrl = String.format("rtc://%s:%d/%s/%s",
//                param.getApp(),
//                param.getStream());
//
//        return new Result<>(webrtcUrl);
    }

    /**
     * 停止流媒体代理
     */
    @ApiOperation(value = "停止流媒体代理")
    @PostMapping("/stopStream")
    public Result<Boolean> stopStream(@ApiParam("流标识") @RequestParam String key) {
        Boolean flag = iApiService.delStreamProxy(key);
        return new Result<>(flag);
    }
}
